<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="query.key" class="handle-input mr10" placeholder="会员ID/邮箱号"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch" class="margin10">搜索
        </el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"  element-loading-background="rgba(0, 0, 0, 0.8)" v-loading="loading"
        header-cell-class-name="table-header">
        <el-table-column prop="uid" label="uId" align="center"></el-table-column>
        <el-table-column prop="avatar" label="头像" align="center">
          <template slot-scope="scope">
            <el-image style="width: 50px; height: 50px"
              :src="'https://wjapi.surveydiscovery.com' + '/' + scope.row.avatar"
              :preview-src-list="['https://wjapi.surveydiscovery.com' + '/' + scope.row.avatar]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="nick_name" label="昵称" align="center" width="100"></el-table-column>
        <el-table-column prop="tel" label="电话" align="center" width="150"></el-table-column>
        <el-table-column prop="zip_code" label="邮编" align="center" width="100"></el-table-column>
        <el-table-column prop="username" label="邮箱" align="center" width="180"></el-table-column>

        <el-table-column prop="gender" label="性别" align="center" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.gender === 1" style="color: #00a854">男</div>
            <div v-if="scope.row.gender === 2" style="color: #f02d2d">女</div>
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="国家" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.country }}
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="州" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.state }}
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="市" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.city }}
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="语言" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.languagesZ }}
          </template>
        </el-table-column>
        
        <el-table-column prop="is_open" label="用户状态" align="center" width="300">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_open"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="封号"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleStatusChange(scope.row)"
              :loading="scope.row.switchLoading"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="create_time" label="操作时间" align="center" width="180"></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
          :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
import { Loading } from "element-ui";
import AxiosPost from "../api/index";

export default {
  name: "DoLogList",
  data() {
    return {
      query: {
        key: '',
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      pageTotal: 0,
      loading: true,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },

    async getData() {
      this.loading = true;
      let info = await AxiosPost("/admin/user/userlist", this.query);
      if (info.code !== 10000) {
        this.$message.error(info.message);
        return false;
      }
      info.data.list.forEach(item => {
        item.create_time = this.$moment(item.create_time * 1000).format('YYYY-MM-DD HH:mm:ss')
        if(item.languages){
          let a = JSON.parse(item.languages);
        let b = []
        a.forEach(item => {
          b.push(item.name_zh);
        })
        item.languagesZ = b.join(',');
        }
        
      })
      this.loading = false;
      this.tableData = info.data.list;
      this.pageTotal = info.data.meta.total;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    
    // 处理用户状态切换
    async handleStatusChange(row) {
      // 设置加载状态
      this.$set(row, 'switchLoading', true);
      
      try {
        const params = {
          uid: row.uid,
          is_open: row.is_open
        };
        
        const result = await AxiosPost("/admin/user/userUpdate", params);
        
        if (result.code === 10000) {
          this.$message.success(row.is_open === 1 ? '用户已启用' : '用户已封号');
        } else {
          // 如果接口调用失败，恢复原状态
          row.is_open = row.is_open === 1 ? 0 : 1;
          this.$message.error(result.message || '操作失败');
        }
      } catch (error) {
        // 如果接口调用失败，恢复原状态
        row.is_open = row.is_open === 1 ? 0 : 1;
        this.$message.error('网络错误，请重试');
        console.error('用户状态更新失败:', error);
      } finally {
        // 清除加载状态
        this.$set(row, 'switchLoading', false);
      }
    },
  },
};
//时间戳转换
</script>

<style scoped>
.handle-input {
  width: 200px;
  display: inline-block;
}

.mr10 {
  margin-right: 10px;
}

.handle-box {
  margin-bottom: 20px;
}

.table {
  font-size: 14px;
}

.margin10 {
  margin-left: 10px;
}
</style>